package org.microdream.easy.deploy.action;

import ice.server.comm.BaseAction;
import ice.server.comm.BaseOperateResult;
import ice.server.comm.ParamBulider;
import org.microdream.easy.deploy.po.DOrg;
import org.microdream.easy.deploy.po.DProject;
import org.microdream.easy.deploy.service.DProjectService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Title: 项目controller
 * @author:沈明可
 * @create: 2019-09-15 20:51
 */
@RestController
@RequestMapping("DProject")
public class ProjectAction extends BaseAction {

    @Resource
    private DProjectService dProjectService;


    /**
     * 功能描述:〈分页查询〉
     *
     * @param limit       页面大小
     * @param offset      当前页码
     * @param projectCode 项目编码 支持模糊查询
     * @param projectName 项目名称 支持模糊查询
     * @param orgCode     机构编码
     * @return : java.util.Map
     * @author : 沈明可
     * @date : 2019/9/15 21:27
     * User :
     */
    @PostMapping("/searchByPage")
    public Map searchByPage(Integer limit, Integer offset, String projectCode, String projectName, String orgCode) {
        if (limit == null || offset == null) {
            return null;
        }
        return dProjectService.searchByPage(ParamBulider.build()
                .addPageAndRow(offset / limit, limit)
                .add("projectCode_cons", projectCode)
                .add("projectName_cons", projectName)
                .add("orgCode", orgCode).get());
    }

    /**
     * 功能描述:〈获取当前登录用户所属机构〉
     *
     * @param
     * @return : java.util.List<org.microdream.easy.deploy.po.DOrg>
     * @author : 沈明可
     * @date : 2019/9/15 20:45
     * User :
     */
    @GetMapping("/getMechanism")
    @Override
    public List<DOrg> getMechanism() {
        return getMechanism();
    }

    /**
     * 功能描述:〈项目新增〉
     *
     * @param dProject 项目实体类
     * @return : ice.server.comm.BaseOperateResult
     * @author : 沈明可
     * @date : 2019/9/15 20:56
     * User :
     */
    @PostMapping("/add")
    public BaseOperateResult add(DProject dProject) {
        return dProjectService.add(dProject, getCurrentLoggedInUser());
    }

    /**
     * 功能描述:〈项目修改〉
     *
     * @param dProject 项目实体类
     * @return : ice.server.comm.BaseOperateResult
     * @author : 沈明可
     * @date : 2019/9/15 20:57
     * User :
     */
    @PutMapping("/update")
    public BaseOperateResult update(DProject dProject) {
        return dProjectService.update(dProject);
    }

    /**
     * 功能描述:〈项目删除〉
     *
     * @param ids 项目id
     * @return : ice.server.comm.BaseOperateResult
     * @author : 沈明可
     * @date : 2019/9/15 20:57
     * User :
     */
    @DeleteMapping("/delete")
    public BaseOperateResult delete(String ids) {
        try {
            dProjectService.delete(ids);
            return BaseOperateResult.delete(true);
        } catch (Exception e) {
            e.printStackTrace();
            return BaseOperateResult.delete(false);
        }
    }
}
